package cn.tedu.csmall.product.service.impl;

import cn.tedu.csmall.commons.ex.ServiceException;
import cn.tedu.csmall.commons.pojo.vo.PageData;
import cn.tedu.csmall.commons.util.PageInfoToPageDataConverter;
import cn.tedu.csmall.commons.web.ServiceCode;
import cn.tedu.csmall.product.mapper.*;
import cn.tedu.csmall.product.pojo.entity.Spu;
import cn.tedu.csmall.product.pojo.entity.SpuDetail;
import cn.tedu.csmall.product.pojo.param.SpuAddNewParam;
import cn.tedu.csmall.product.pojo.vo.*;
import cn.tedu.csmall.product.service.SpuService;
import cn.tedu.csmall.product.util.IdUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class SpuServiceImpl implements SpuService {
    @Autowired
    IdUtils idUtils;
    @Autowired
    BrandMapper brandMapper;
    @Autowired
    CategoryMapper categoryMapper;
    @Autowired
    AlbumMapper albumMapper;
    @Autowired
    SpuMapper spuMapper;
    @Autowired
    SpuDetailMapper spuDetailMapper;

    @Override
    public void addNew(SpuAddNewParam spuAddNewParam) {
        log.debug("开始处理【新增SPU】的业务，参数:{}",spuAddNewParam);

        Long brandId = spuAddNewParam.getBrandId();
        //检查品牌是否存在，是否启用
        BrandStandardVo brandStandardVo = brandMapper.getStandardById(brandId);
        if (brandStandardVo==null){
            String message = "新增SPU失败，所属品牌不存在!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
        if (brandStandardVo.getEnable() == 0){
            String message = "新增Spu失败，所属品牌已禁用!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
        }

        //检查类别：是否存在，是否启用，是否包含子级类别
        Long categoryId = spuAddNewParam.getCategoryId();
        CategoryStandardVo categoryStandardVo = categoryMapper.getStandardById(categoryId);
        if (categoryStandardVo == null){
            String message = "新增SPU失败，所属类别不存在!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
        if (categoryStandardVo.getEnable() ==0){
            String message = "新增spu失败，所属类别已禁用!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
        }
        if (categoryStandardVo.getIsParent()==1){
            String message = "新增spu失败，所属类别存在子级类别!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
        }

        //检查相册：是否存在
        Long albumId = spuAddNewParam.getAlbumId();
        AlbumStandardVo albumStandardVo = albumMapper.getStandardById(albumId);
        if (albumStandardVo==null){
            String message = "新增spu失败,所属相册不存在!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }

        //生成spu的id
        Long spuId = idUtils.generateSpuId();
        Spu spu = new Spu();
        BeanUtils.copyProperties(spuAddNewParam,spu);
        // 补全属性值：id >>> 暂时随便写
        spu.setId(spuId);
        // 补全属性值：brand_name, category_name >>> 此前检查时的查询结果
        spu.setBrandName(brandStandardVo.getName());
        spu.setCategoryName(categoryStandardVo.getName());
        // 补全属性值：sales, comment_count, positive_comment_count >>> 0
        spu.setSales(0);
        spu.setCommentCount(0);
        spu.setPositiveCommentCount(0);
        // 补全属性值：is_deleted, is_published, is_new_arrival, is_recommend >>> 0
        spu.setIsDeleted(0);
        spu.setIsPublished(0);
        spu.setIsNewArrival(0);
        spu.setIsRecommend(0);
        // 补全属性值：is_checked >>> 0, check_user >>> null, gmt_check >>> null
        spu.setIsChecked(0);
        // 插入SPU数据
        int rows = spuMapper.insert(spu);
        if (rows != 1) {
            String message = "新增SPU失败，服务器忙，请稍后再尝试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_INSERT, message);
        }

        // 创建SpuDetail对象
        SpuDetail spuDetail = new SpuDetail();
        // 补全属性值：spu_id >>> 与以上Spu的ID相同
        spuDetail.setSpuId(spuId);
        // 补全属性值：detail >>> 来自参数
        spuDetail.setDetail(spuAddNewParam.getDetail());
        // 插入SpuDetail数据
        rows = spuDetailMapper.insert(spuDetail);
        if (rows != 1) {
            String message = "新增SPU失败，服务器忙，请稍后再尝试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_INSERT, message);
        }
    }

    @Override
    public PageData<SpuListItemVO> list(Integer pageNum) {
        Integer pageSize = 10;
        return list(pageNum,pageSize);
    }

    @Override
    public PageData<SpuListItemVO> list(Integer pageNum, Integer pageSize) {
        log.debug("开始处理【查询SPU列表】的业务,页码:{}，每页条数:{}",pageNum,pageSize);
        PageHelper.startPage(pageNum,pageSize);
        List<SpuListItemVO> list = spuMapper.list();
        PageInfo<SpuListItemVO> pageInfo = new PageInfo<>(list);
        PageData<SpuListItemVO> pageData = PageInfoToPageDataConverter.convert(pageInfo);
        return pageData;
    }

    @Override
    public SpuStandardVO getStandardById(Long id) {
        log.debug("开始处理【根据ID查询SPU基本信息】的业务，参数:{}",id);
        return spuMapper.getStandardById(id);
    }
}
